package com.pureut.storage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pureut.storage.domain.WipCompletion;
import com.pureut.storage.domain.WipCompletionDetail;
import com.pureut.storage.dto.WipCompletionDetailDto;
import com.pureut.storage.mapper.WipCompletionDetailMapper;
import com.pureut.storage.mapper.WipCompletionMapper;
import com.pureut.storage.service.WipCompletionDetailService;
import com.sale.common.core.constant.CacheNames;
import com.sale.common.mybatis.core.page.PageQuery;
import com.sale.common.mybatis.core.page.TableDataInfo;
import com.sale.common.redis.utils.CacheUtils;
import com.sale.system.api.RemoteUnitService;
import com.sale.system.api.domain.SysAuxiliary;
import com.sale.system.api.domain.SysDictData;
import com.sale.system.api.model.MaterialUnitDto;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/***
 * 完工入库明细实现层
 * Author:C
 * Date:2023/01/29 13:51
 * @DESC
 */
@Service
public class WipCompletionDetailServiceImpl implements WipCompletionDetailService {

    @Resource
    WipCompletionDetailMapper wipCompletionDetailMapper;

    @Resource
    WipCompletionMapper wipCompletionMapper;

    @DubboReference
    RemoteUnitService remoteUnitService;

    /**
     * 完工入库明细列表
     *
     * @param id
     * @return
     */
    @Override
    public TableDataInfo<WipCompletionDetailDto> getWipCompletionDetail(long id, PageQuery pageQuery) {

        //完工入库明细状态
        List<SysDictData> detailStatusArray = CacheUtils.get(CacheNames.SYS_DICT, "wip_completion_detail_status");
        Map<String, String> detailStatusMap = detailStatusArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        Page<WipCompletionDetailDto> wipCompletionDetailList = wipCompletionDetailMapper.getWipCompletionDetailList(pageQuery.build(), id);
        List<WipCompletionDetailDto> records = wipCompletionDetailList.getRecords();

        WipCompletion wipCompletion = wipCompletionMapper.selectOne(new QueryWrapper<WipCompletion>().lambda().eq(WipCompletion::getId, id));
        //单位集合
        Map<String, MaterialUnitDto> unitByCodeToMap = remoteUnitService.getUnitByCodeToMap();

        for (WipCompletionDetailDto entity : records) {
            //辅助属性
            List<SysAuxiliary> statsArray = CacheUtils.get(CacheNames.SYS_GINSENG + "_" + wipCompletion.getTenantId(), String.valueOf(entity.getMaterialId()));
            entity.setAuxiliaryAttribute(statsArray);
            //状态
            entity.setStatusDict(detailStatusMap.get(entity.getStatus()));
            //单位
            if (entity.getMaterialUnit() != null) {
                entity.setUnitDict(unitByCodeToMap.get(entity.getMaterialUnit()).getUnitName());
            }
        }
        return TableDataInfo.build(wipCompletionDetailList);
    }

    /**
     * 批量保存数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveBatchWipCompletionDetail(List<WipCompletionDetail> list) {
        return wipCompletionDetailMapper.insertBatch(list);
    }
}
